package gui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridLayout;
import javax.swing.SwingConstants;

import model.DelBehandlingDragering;
import model.DelBehandlingPakkeKlar;
import model.DelBehandlingTorreTid;

import service.Service;

import java.awt.Font;
import java.util.GregorianCalendar;
/**
 * 
 * @author Martin Sax og Daniel Nissen
 *
 */
public class Statestik extends JPanel implements CarlettiPanel {
	private JPanel panelEast;
	private JPanel panelWest;
	private JLabel lblAntalProduceredeVarer;
	private JLabel lblAntalSpildVarer;
	private JLabel lblAntalVarerP;
	private JLabel lblAntalPakkeklarVarer;
	private JLabel lblAntalVarerI;
	private JPanel panelCenter;
	private JLabel lblNavn;
	private JLabel lblAntal;
	private JLabel lblProduceredeVarer;
	private JLabel lblSpildVarer;
	private JLabel lblLagerVarer;
	private JLabel lblPakkeklarVarer;
	private JLabel lblVarerIProcess;
	private JLabel lblEffektivetet;
	private JLabel lblProcentProduceredeVarer;
	private JLabel lblProcentSpildVarer;
	private JLabel lblProcentVarerP;
	private JLabel lblProcentPakkeklarVarer;
	private JLabel lblProcentVarerI;
	private JLabel lblGennemSnitP;
	private JLabel lblProcentAfTid;
	private JLabel lblTidPaaLager;

	/**
	 * Create the panel.
	 */
	public Statestik() {
		setLayout(new GridLayout(0, 3, 0, 0));

		panelEast = new JPanel();
		add(panelEast);
		panelEast.setLayout(new GridLayout(0, 1, 0, 0));

		lblNavn = new JLabel("Navn");
		lblNavn.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		lblNavn.setHorizontalAlignment(SwingConstants.CENTER);
		panelEast.add(lblNavn);

		lblAntalProduceredeVarer = new JLabel("Antal producerede varer:");
		panelEast.add(lblAntalProduceredeVarer);

		lblAntalSpildVarer = new JLabel("Antal spild varer:");
		panelEast.add(lblAntalSpildVarer);

		lblAntalVarerP = new JLabel("Antal varer på lager:");
		panelEast.add(lblAntalVarerP);

		lblAntalPakkeklarVarer = new JLabel("Antal Pakkeklar varer:");
		panelEast.add(lblAntalPakkeklarVarer);

		lblAntalVarerI = new JLabel("Antal varer i process udenfor lager:");
		panelEast.add(lblAntalVarerI);

		lblTidPaaLager = new JLabel("Tid brugt på lager");
		panelEast.add(lblTidPaaLager);

		panelCenter = new JPanel();
		add(panelCenter);
		panelCenter.setLayout(new GridLayout(0, 1, 0, 0));

		lblAntal = new JLabel("Antal");
		lblAntal.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		lblAntal.setHorizontalAlignment(SwingConstants.CENTER);
		panelCenter.add(lblAntal);

		lblProduceredeVarer = new JLabel("Producerede varer");
		lblProduceredeVarer.setHorizontalAlignment(SwingConstants.CENTER);
		panelCenter.add(lblProduceredeVarer);

		lblSpildVarer = new JLabel("Spild varer");
		lblSpildVarer.setHorizontalAlignment(SwingConstants.CENTER);
		panelCenter.add(lblSpildVarer);

		lblLagerVarer = new JLabel("Lager varer");
		lblLagerVarer.setHorizontalAlignment(SwingConstants.CENTER);
		panelCenter.add(lblLagerVarer);

		lblPakkeklarVarer = new JLabel("Pakkeklar varer");
		lblPakkeklarVarer.setHorizontalAlignment(SwingConstants.CENTER);
		panelCenter.add(lblPakkeklarVarer);

		lblVarerIProcess = new JLabel("Varer i process");
		lblVarerIProcess.setHorizontalAlignment(SwingConstants.CENTER);
		panelCenter.add(lblVarerIProcess);

		lblGennemSnitP = new JLabel("Gennem snit på lager");
		lblGennemSnitP.setHorizontalAlignment(SwingConstants.CENTER);
		panelCenter.add(lblGennemSnitP);

		panelWest = new JPanel();
		add(panelWest);
		panelWest.setLayout(new GridLayout(0, 1, 0, 0));

		lblEffektivetet = new JLabel("Effektivetet");
		lblEffektivetet.setHorizontalAlignment(SwingConstants.CENTER);
		lblEffektivetet.setFont(new Font("Lucida Grande", Font.BOLD, 15));
		panelWest.add(lblEffektivetet);

		lblProcentProduceredeVarer = new JLabel("Procent producerede varer");
		lblProcentProduceredeVarer
				.setHorizontalAlignment(SwingConstants.CENTER);
		panelWest.add(lblProcentProduceredeVarer);

		lblProcentSpildVarer = new JLabel("Procent spild varer");
		lblProcentSpildVarer.setHorizontalAlignment(SwingConstants.CENTER);
		panelWest.add(lblProcentSpildVarer);

		lblProcentVarerP = new JLabel("Procent varer på lager");
		lblProcentVarerP.setHorizontalAlignment(SwingConstants.CENTER);
		panelWest.add(lblProcentVarerP);

		lblProcentPakkeklarVarer = new JLabel("Procent pakkeklar varer");
		lblProcentPakkeklarVarer.setHorizontalAlignment(SwingConstants.CENTER);
		panelWest.add(lblProcentPakkeklarVarer);

		lblProcentVarerI = new JLabel("Procent varer i process");
		lblProcentVarerI.setHorizontalAlignment(SwingConstants.CENTER);
		panelWest.add(lblProcentVarerI);

		lblProcentAfTid = new JLabel("Procent af tid på lager");
		lblProcentAfTid.setHorizontalAlignment(SwingConstants.CENTER);
		panelWest.add(lblProcentAfTid);

	}

	public void update() {
		double spild = 0;
		double pakkeklar = 0;
		double varepaalager = 0;
		double process = 0;
		double produktion = 0;
		double friLagerp = 0;
		double tidpaalager = 0;
		double tidbrugt = 0;
		double antalfaerdigevarer = 0;
		for (model.MellemVare vare : Service.getAllMellemVare()) {
			int i = 0;
			if(vare.getAllTimeStamp()[ vare.getAllTimeStamp().length-1] != null){
				antalfaerdigevarer++;
				tidbrugt += vare.getAllTimeStamp()[ vare.getAllTimeStamp().length-1].getStartTidsPunkt().getTimeInMillis() -vare.getAllTimeStamp()[0].getStartTidsPunkt().getTimeInMillis();
			}
			
			while(i < vare.getAllTimeStamp().length && vare.getAllTimeStamp()[vare.getAllTimeStamp().length-1] != null){
								if(i >= 1 && vare.getAllTimeStamp()[i-1].getDelBehandling() instanceof DelBehandlingTorreTid && vare.getAllTimeStamp()[i].getDelBehandling() instanceof DelBehandlingPakkeKlar){
					tidpaalager += vare.getAllTimeStamp()[i].getStartTidsPunkt().getTimeInMillis() - vare.getAllTimeStamp()[i-1].getStartTidsPunkt().getTimeInMillis();
		
				}
				if(i == vare.getAllTimeStamp().length-2){
					
					tidpaalager += vare.getAllTimeStamp()[i+1].getStartTidsPunkt().getTimeInMillis() - vare.getAllTimeStamp()[i].getStartTidsPunkt().getTimeInMillis();
				}else if(vare.getAllTimeStamp()[i].getDelBehandling() instanceof DelBehandlingDragering){
					tidpaalager += vare.getAllTimeStamp()[i].getStartTidsPunkt().getTimeInMillis() - vare.getAllTimeStamp()[i-1].getStartTidsPunkt().getTimeInMillis();
				
				}
				i++;
				}
			
			if (vare.getAllTimeStamp()[vare.getAllTimeStamp().length - 2] != null
					&& vare.getAllTimeStamp()[vare.getAllTimeStamp().length - 1] == null) {
				pakkeklar++;
			}
			if (vare.getTimeStamp().getDelBehandling() instanceof model.DelBehandlingDragering) {
				process++;
			}
			
			if (new GregorianCalendar().get(GregorianCalendar.MONTH) == vare
					.getAllTimeStamp()[0].getStartTidsPunkt().get(
					GregorianCalendar.MONTH)
					&& new GregorianCalendar().get(GregorianCalendar.YEAR) == vare
							.getAllTimeStamp()[0].getStartTidsPunkt().get(
							GregorianCalendar.YEAR)) {
				produktion++;
				if (vare.getAllTimeStamp()[vare.getAllTimeStamp().length - 1] != null) {
					if (vare.getAllTimeStamp()[vare.getAllTimeStamp().length - 1]
							.getStartTidsPunkt().getTimeInMillis()
							- vare.getAllTimeStamp()[vare.getAllTimeStamp().length - 2]
									.getStartTidsPunkt().getTimeInMillis() > (vare
							.getTimeStamp().getDelBehandling().getMaxTid() * 1000)) {
						spild++;
					}
				}
				
			}
		}
			
		

		for (model.LagerPlads lp : Service.getLager().getLagerPladser()) {
			if (lp.getMellemVare() != null) {
				varepaalager++;
			}else{
				friLagerp++;
			}
			
		}
			
		lblProduceredeVarer.setText(Math.round(produktion) + "");
		lblSpildVarer.setText("" + Math.round(spild));
		lblPakkeklarVarer.setText("" + Math.round(pakkeklar));
		lblLagerVarer.setText("" + Math.round(varepaalager));
		lblVarerIProcess.setText("" + Math.round(process));
		lblGennemSnitP.setText("" + Math.round((tidpaalager/ antalfaerdigevarer )/1000) + "sek");
		lblProcentProduceredeVarer.setText(Math.round(produktion / (30*new GregorianCalendar().get(GregorianCalendar.DAY_OF_MONTH))* 100) + "%");
		lblProcentSpildVarer.setText("" + Math.round((spild / produktion)*100) + "%");
		lblProcentVarerP.setText(Math.round((varepaalager / Service.getLager().getAntalPladser()) *100) + "%");
		lblProcentPakkeklarVarer.setText(Math.round((pakkeklar/varepaalager)*100) + "%");
		lblProcentVarerI.setText(Math.round((process/friLagerp)*100) + "%");
		lblProcentAfTid.setText("" + Math.round((tidpaalager/tidbrugt) * 100) + "%");

	}
}
